package session;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.junit.Test;


public class SessionTests {

	@Test
	public void testPackSessionState() throws IOException {
		SessionID sessId = new SessionID("123456789");
		Session sess = new Session(sessId, 5678, "this is a message", 5);
		byte[] pack = Session.packSessionState(sess);
		Session sessUnpacked = Session.unpackSessionState(pack);
		assertEquals(sess.getDiscard_time(), sessUnpacked.getDiscard_time());
		assertEquals(sess.getMessage(), sessUnpacked.getMessage());
		assertEquals(sess.getSessionID(), sessUnpacked.getSessionID());
		assertEquals(sess.getSessionVersion(), sessUnpacked.getSessionVersion());
	}
	
	@Test
	public void testURIEncoding() throws UnsupportedEncodingException {
		String uri = URLEncoder.encode(":-_&", "UTF-8");
		System.out.println("testUrlEncoding: " + uri);
		String url_dec = URLDecoder.decode(uri, "UTF-8");
		System.out.println("testUrlEncoding: " + url_dec);
	}
	
	@Test
	public void sendUDPPacket() {
        new Thread(
        		new Runnable() {
        			public void run() {
        				while (true) {
	        				try {
	        		        DatagramSocket serverSocket = new DatagramSocket(9876);
	        		        byte[] receiveData = new byte[1024];
	        		        byte[] sendData = new byte[1024];
	        	              DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
	        	              serverSocket.receive(receivePacket);
	        	              //String sentence = new String( receivePacket.getData());
	        	              ObjectInputStream iStream = new ObjectInputStream(new ByteArrayInputStream(receivePacket.getData()));
	        	              Session sess = (Session) iStream.readObject();
	        	              System.out.println("Session ID:" + sess.getSessionID());
	        	           
	        	              iStream.close();
	        	              
	        	              /*
	        	              System.out.println("RECEIVED: " + sentence);
	        	              InetAddress IPAddress = receivePacket.getAddress();
	        	              int port = receivePacket.getPort();
	        	              String capitalizedSentence = sentence.toUpperCase();
	        	              sendData = capitalizedSentence.getBytes();
	        	              DatagramPacket sendPacket =
	        	              new DatagramPacket(sendData, sendData.length, IPAddress, port);
	        	              serverSocket.send(sendPacket);
	        	              */
	        				}
	        				catch (ClassNotFoundException e) {
	        					//do nothing
	        				} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
        				}
        			}
        		}
        ).start();
        
        new Thread(
        		new Runnable() {
        			public void run() {
        				while (true) {
	        				try {
	        					Session sess = new Session(new SessionID("lol"), 30, "testsession");
	        					ByteArrayOutputStream bStream = new ByteArrayOutputStream();
	        					ObjectOutput oo = new ObjectOutputStream(bStream); 
	        					oo.writeObject(sess);
	        					oo.close();
	        					byte[] serializedMessage = bStream.toByteArray();
	        					System.out.println("Size of session: " + serializedMessage.length + " bytes");
	        				    //BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        				        DatagramSocket clientSocket = new DatagramSocket();
        				        InetAddress IPAddress = InetAddress.getByName("localhost");
        				        byte[] sendData = new byte[1024];
        				        byte[] receiveData = new byte[1024];
        				        //String sentence = "testsent";
        				        //sendData = sentence.getBytes();
        				        sendData = serializedMessage;
        				        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
        				        clientSocket.send(sendPacket);
        				        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        				        clientSocket.receive(receivePacket);
        				        String modifiedSentence = new String(receivePacket.getData());
        				        System.out.println("FROM SERVER:" + modifiedSentence);
        				        clientSocket.close();
	        				}
	        				catch (IOException e) {
	        					e.printStackTrace();
	        				}
        				}
        			}
        		}
        ).start();
        int wait = 5;
        for (int i = 0; i < wait; i++ ) {
        	try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
	}

}
